package com.cw.websocket;

 
import com.cw.common.constant.*;
import com.cw.config.service.impl.*;
import java.util.*;
import javax.servlet.http.*;
import org.springframework.http.server.*;
import org.springframework.web.socket.*;
import org.springframework.web.socket.server.*;

/**
 * @author sheree Date 2020/7/24
 * @Description
 */
public class WebSocketInterceptor implements HandshakeInterceptor {

    private RedisServiceImpl redisService;

    public WebSocketInterceptor(RedisServiceImpl redisService) {
	  this.redisService = redisService;
    }

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
					     WebSocketHandler handler, Map<String, Object> map)
	  throws Exception {
	  final ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
	  final HttpSession session = servletRequest.getServletRequest().getSession();
	  if (session != null) {
		if (null != redisService.get(RedisConstant.wsLoginUser)) {
		    map.put("user", redisService.get(RedisConstant.wsLoginUser).toString());
		}
	  }
	  return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
					 WebSocketHandler handler, Exception e) {

    }
}
